package com.gxgeek.order.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gxgeek.order.dataobject.OrderDetail;
import com.gxgeek.order.dto.OrderDTO;
import com.gxgeek.order.enums.ResultEnum;
import com.gxgeek.order.exception.SellException;
import com.gxgeek.order.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gx on 2017/7/13.
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderDTO convert(OrderForm orderForm){
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        Gson gson = new Gson();
        List <OrderDetail> orderDetailList;
        try {
            orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            log.error("【对象转换】对象转化错误String{}",orderForm.getItems());
            throw new SellException(ResultEnum.PAYAM_ERROR);
        }
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }

}
